/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : reserve_ok.js                                 
*  Description  : 실시간 예약 결제 완료
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-07-26  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	
	// 예약정보
	function clsReserveInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsReserveInfo.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.reserve_view_bgc");

			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("aside.right_view ul.r_event_list li:first").clone(true)
			}
			
			this.setInfo({
				"이벤트리스트": []
			});
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
			this.setPDate();
		}
		, display: function(){
			var $layer = this.$getLayer();
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.find("p.golf_d span:eq(0)").html(this.infoData["골프장명"] + " " + this.infoData["코스명"]);
			$layer.find("p.golf_d span:eq(1)").html(this.infoData["홀명"]);
			$layer.find("p.res_date").contents().eq(0).before(this.infoData["날짜"] + ". " + this.infoData["요일"] + "요일").remove();
			$layer.find("p.res_date span").html(this.infoData["시간"]);
			$layer.find("div.res_price div.cost span").html(_util.number_format(this.infoData["원가"]));
			$layer.find("div.res_price div.sale p.s_price span").html(_util.number_format(this.infoData["가격"]));
			
			if(this.infoData["원가"] == this.infoData["가격"]){
				$layer.find("div.res_price div.sale p.ma_right20").html("정상그린피");
				$layer.find("div.res_price>div:lt(2)").hide();
			}
			
			$layer.find("div.amount strong.ft_b").html(this.infoData["보증결제금액"] ? _util.number_format(this.infoData["보증결제금액"]):"");
			
			$layer.find("ul.ico_list img").each(function(n){
				var s = $(this).attr("src");
				$(this).attr("src", s.replace("_off", "").replace(".png", "_off.png"));
				if(thisClass.infoData["이벤트"] != null){
					if(thisClass.infoData["이벤트"].indexOf($(this).attr("alt")) != -1) $(this).attr("src", s.replace("_off", ""));
				}
			});
			
			if(this.infoData["이벤트리스트"].length){
				$layer.find("aside.right_view article.board_list_none").hide();
				
				$layer.find("aside.right_view ul.r_event_list").empty();
				for(var i=0;i<this.infoData["이벤트리스트"].length;i++){
					$layer.find("aside.right_view ul.r_event_list").append($element.clone(true));
				}
				$layer.find("aside.right_view ul.r_event_list li").each(function(n){
					$(this).html(thisClass.infoData["이벤트리스트"][n]);
				});
			}else{
				$layer.find("aside.right_view article.board_list_none").show();
				$layer.find("aside.right_view ul.r_event_list").hide();
				
				$layer.find("aside.right_view p.tit_event").hide();
			}
		}
		, spinStart: function(){
			var $layer = this.$getLayer();
			
			$layer.find("div.reserve_view").children().hide();
			if($layer.find("div.reserve_view div#tmpSpin").length == 0){
				$layer.find("div.reserve_view").append("<div id='tmpSpin' style='position:relative; width:700px; height:150px;' />");
			}
			$layer.find("div.reserve_view div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this.$getLayer();
			
			$layer.find("div.reserve_view").children().show();
			$layer.find("div.reserve_view div#tmpSpin").hide().spin(false);
		}
		, setPDate: function(){
			var $layer = this.$getLayer();
			
			if(!this.infoData["가승인결제마감일"]) return;
			
			$layer.parents("section#main_content").find("p.reserve_txt02").html("<em>보증결제 마감일</em>은 <em>" + this.infoData["가승인결제마감일"].getFullYear()+ "년</em> <strong><font class='ft_r'>" + (this.infoData["가승인결제마감일"].getMonth()+1) + "</font>월 <font class='ft_r'>" + this.infoData["가승인결제마감일"].getDate() + "</font>일</strong> 입니다.");
		}
	}
	
	function clsComm()
	{
		
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
		// 예약정보
		, getReserveInfo: function(arg){
			var thisClass = this;
			
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "getReservView";
			_gf.ajax({
				"sid": "RS001"
				, "servicename": serviceName
			}, {
				"reserv_seq": arg["예약코드"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
					, "결과값": {
						"예약정보": {}
						, "코스정보": {}
					}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					
					var reserv_date = _util.nvl(obj["reserv__date"])+"";
					var reserv_time = _util.nvl(obj["reserv__time"])+"";
					var szEvent = "";
					var pDate = new Date(reserv_date.substr(0, 4), reserv_date.substr(4, 2)-1, reserv_date.substr(6, 2));
					pDate.setDate(pDate.getDate()-5);
					
					var e = [];
										
					if(obj["greenfee__use__yn"] == "Y"){
						szEvent = "그린피";
						e.push("그린피 할인: " + obj["greenfee__discount__amount"] +  _util.nvl(obj["greenfee_amt_type"]));
					}
					
//					if(obj["mileage__use__yn"] == "Y"){
//						e.push("마일리지: " + obj["mileage__amount"] +  obj["mileage__type"]);
//					}
					
					if(obj["cartfee__use__yn"] == "Y"){
						if(szEvent) szEvent += ",";
						szEvent += "카트피";
						e.push("카트피: " + obj["cartfee__discount__amount"]);
					}
					
					if(obj["food__use__yn"] == "Y"){
						if(szEvent) szEvent += ",";
						szEvent += "식사";
						e.push("식사: " + obj["food__discount__amount"]);
					}
					
					if(obj["present__use__yn"] == "Y"){
						if(szEvent) szEvent += ",";
						szEvent += "선물";
						e.push("선물: " + obj["present__content"]);
					}
					
					if(obj["etc__use__yn"] == "Y"){
						if(szEvent) szEvent += ",";
						szEvent += "기타";
						e.push("기타: " + obj["etc__content"]);
					}
					
					res["결과값"]["예약정보"] = {
						"예약코드": arg["예약코드"]
						, "코스코드": "001"
						, "골프장명": _util.nvl(obj["golfclub__name"])
						, "코스명": _util.nvl(obj["course__info__name1"])
						, "홀명": _util.nvl(obj["hole"]) + "H"
						, "날짜": reserv_date.substr(0, 4) + ". " + reserv_date.substr(4, 2) + ". " + reserv_date.substr(6, 2)
						, "요일": _util.nvl(obj["reserv__day"])
						, "시간": reserv_time.substr(0, 2) + ":" + reserv_time.substr(2, 2)
						, "원가": _util.nvl(obj["price"], 0)
						, "가격": _util.nvl(obj["greenfee"], _util.nvl(obj["price"], 0))
						, "이벤트": szEvent
						, "보증결제금액": _util.nvl(obj["price__pay"], 0)
						, "예약상태": obj["reserv__status"]
						, "결제등급": obj["reserv__paytype"]
						, "가승인결제마감일": pDate
						, "이벤트리스트": e
					}
					
					res["결과값"]["코스정보"] = {
						"코스알림": _util.nvl(obj["course__notice"])
						, "코스명": _util.nvl(obj["course__info__name1"]) + ((obj["course__info__name2"]) ? ", " +obj["course__info__name2"]:"")
						, "상세정보": _util.nvl(obj["description"])
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objBookingControl = null;
		this.objReserveInfo = null;
		
		this.q_reserve = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objBookingControl = new clsBookingControl();
			this.objReserveInfo = new clsReserveInfo();
		
			this._bind();
			this._beforeStart();
		}
		, _preload: function(){
			_gf.spinStart();
			
			this.q_reserve = _util.getParam("q_reserve");
			
		}
		, _beforeStart: function(){
			var thisClass = this;
			
			this._permitStart = {
				"좌메뉴": false
			}
			
			_util.executeThread(function(){
				return thisClass.objBookingControl.isLoad();
			}, function(){
				thisClass._permitStart["좌메뉴"] = true;
			});
			
			_util.executeThread(function(){
				for(var key in thisClass._permitStart){
					if(!thisClass._permitStart[key]) return false;
				}
				
				return true;
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
			
			
		}
		, _bind: function(){
			var thisClass = this;
			
			// 달력 선택시
			$(this.objBookingControl).on("calendarChange", null, function(){
				thisClass.objBookingControl.goMainPage();
			});
			
			// 실시간 예약 검색 홈으로
			$("div#container section#main_content").on("click", "img[alt='실시간 예약 검색 홈으로'], img[alt='sms알림'], img[alt='e-mail알림']", function(){
				var alt = $(this).attr("alt");
				
				switch(alt){
					case "실시간 예약 검색 홈으로": 
						thisClass.objBookingControl.goReserveTimePage();
						break;
					case "sms알림":
						thisClass.goSmsAlimPage(this.q_reserve);
						break;
					case "e-mail알림":
						thisClass.goEmailAlimPage(this.q_reserve);
						break;
				}
			});
		}
		, _start: function(){
			var thisClass = this;
			
			this.objBookingControl._start(this);
			
			this.objReserveInfo.spinStart();
			this.objComm.getReserveInfo({
				"예약코드": this.q_reserve
				, "콜백": function(ret){
					thisClass.finishReserveInfo(ret);
				}
			});
		}
		// 예약정보 완료
		, finishReserveInfo: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
				
			this.objReserveInfo.spinEnd();
			this.objReserveInfo.setInfo(ret["결과값"]["예약정보"]);
		}
		// 이메일 알림 이동
		, goEmailAlimPage: function(q_reserve){
			var szLink = _ROOT + "/popup/pop_email.html?q_reserve=" + this.q_reserve;
			var winEmailAlim = window.open(szLink, "winEmailAlim", "width=1000, height=600, scrollbars=0");
			if(winEmailAlim) winEmailAlim.focus();
		}
		// sms 발송 이동
		, goSmsAlimPage: function(q_reserve){
			var szLink = _ROOT + "/popup/pop_sms.html?q_reserve=" + this.q_reserve;
			var winSms = window.open(szLink, "winSms", "width=600, height=500, scrollbars=0");
			if(winSms) winSms.focus();
		}
	}
	
	$.ajax({
		url: _ROOT + "/booking/js/booking_common.js"
		, type: "get"
		, dataType: "script"
		, async: false
		, success: function(ret){
			document.objControl = new clsControl();
			document.objControl._init();
		}
		, error: function(){
			alert("error");
		}
	});
});
